package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketRecordDao;
import com.bestcem.xm.ticket.dao.converter.TicketRecordConverter;
import com.bestcem.xm.ticket.dao.mapper.TicketRecordDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.entity.mysql.TicketRecordDO;
import com.bestcem.xm.ticket.enums.TicketEventEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 15:10
 */
@Slf4j
@Repository
public class TicketRecordDaoImpl implements TicketRecordDao {
    @Resource
    private TicketRecordDaoMapper ticketRecordDaoMapper;

    @Override
    public TicketRecord findById(String id) {
        TicketRecordDO ticketRecordDO = ticketRecordDaoMapper.findByTicketRecordId(id);
        return TicketRecordConverter.doToDto(ticketRecordDO);
    }

    @Override
    public TicketRecord findLastOneByTicketId(String ticketId) {
        TicketRecordDO ticketRecordDO = ticketRecordDaoMapper.findByTicketId(ticketId);

        return TicketRecordConverter.doToDto(ticketRecordDO);
    }

    @Override
    public TicketRecord findFollowByTicketId(String ticketId) {
        TicketRecordDO ticketRecordDO = ticketRecordDaoMapper
                .findByTicketIdAndEvent(ticketId, TicketEventEnum.COMMIT_FOLLOWED.getIndex());

        return TicketRecordConverter.doToDto(ticketRecordDO);
    }

    @Override
    public TicketRecord findTicketFollowRecordByEvent(String ticketId, Integer event, LocalDateTime createTime) {
        TicketRecordDO ticketRecordDO = ticketRecordDaoMapper
                .findByTicketIdAndEventAndCreateTime(ticketId, event, createTime);

        return TicketRecordConverter.doToDto(ticketRecordDO);
    }

    @Override
    public TicketRecord saveTicketRecord(TicketRecord entity) {
        TicketRecordDO entityDo = TicketRecordConverter.dtoToDo(entity);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (ObjectUtils.isEmpty(entityDo.getTicketRecordId())) {
            entityDo.setTicketRecordId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        ticketRecordDaoMapper.insert(entityDo);

        return TicketRecordConverter.doToDto(entityDo);
    }

    @Override
    public List<TicketRecord> listByTicketId(String ticketId) {
        List<TicketRecordDO> ticketRecordDOList = ticketRecordDaoMapper.listByTicketId(ticketId);

        return TicketRecordConverter.doListToDtoList(ticketRecordDOList);
    }
}
